package sc.tool.sqlgen.branchinfo.pfs;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;

import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import sc.prototype.common.FileUtil;
import sc.tool.sqlgen.branchinfo.DBUtil;
import sc.tool.sqlgen.branchinfo.XlsxUtil;

public class BankDuplicateConfirm {

    String branchFilePath = "D:/WeibopayMemo/出款服务FOS/联行号信息.xml";

    String sheetFilePath = "D:/WeibopayMemo/出款服务FOS/分支行重复20130104.xlsx";
    String sheetName = "重复分支行";

    String outputFilePath = "D:/WeibopayMemo/出款服务FOS/分支行重复20130104_处理结果.xlsx";

    String branchFileContent = null;

    void loadBranchFile() {
        try {
            branchFileContent = FileUtil.getFileAsString(branchFilePath, "GB2312");
        } catch (IOException e) {
            throw new RuntimeException("", e);
        }
    }

    Sheet loadSheet() {
        try {
            InputStream inp = new FileInputStream(sheetFilePath);
            Workbook wb = new XSSFWorkbook(inp);
            return wb.getSheet(sheetName);
        } catch (Exception e) {
            throw new RuntimeException("", e);
        }
    }

    boolean existInBranchFile(String branchNo) {
        return branchFileContent.contains("'" + branchNo + "'");
    }

    SimpleStatistic statistic = new SimpleStatistic();

    DBUtil dbUtil = new DBUtil();

    public void process() {
        loadBranchFile();
        Sheet sheet = loadSheet();
        Row row = null;
        String branchNo = null, branchName = null;
        boolean exist = false;
        String writeExist = null;
        for (Iterator<Row> iter = sheet.rowIterator(); iter.hasNext();) {
            row = iter.next();
            if (row.getRowNum() == 0) {
                continue;
            }
            statistic.addStatistic("处理条数");
            branchNo = row.getCell(0).getStringCellValue();
            branchName = row.getCell(1).getStringCellValue();
            exist = existInBranchFile(branchNo);
            writeExist = exist ? "是" : "否";
            row.createCell(2).setCellValue(writeExist);
            if (exist) {
                statistic.addStatistic("联行号存在");
            } else {
                statistic.addStatistic("联行号不存在");
            }
        }
        XlsxUtil.writeFile(outputFilePath, sheet.getWorkbook());
        statistic.printStatistic();
    }

    public static void main(String[] args) {
        new BankDuplicateConfirm().process();
    }
}
